// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ConsumerProducerAgent.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Microsoft.Robotics.Runtime
{
    using System.Runtime.Serialization;

    /// <summary>
    /// Base agent class that provides the ability to consume one message type and generate another message type. 
    /// The agent can chose to publish 0, 1 or many messages in response to an input message.
    /// </summary>
    /// <typeparam name="TInput">The type of input messages accepted by the consumer part of the agent.</typeparam>
    /// <typeparam name="TOutput">The type of messages published by this agent.</typeparam>
    [DataContract]
    public abstract class ConsumerProducerAgent<TInput, TOutput> : ProducerAgent<TOutput>, ISubscriptionReceiver<TInput>
        where TInput : AgentMessage where TOutput : AgentMessage
    {
        /// <summary>
        /// The name of the agent we subscribe to.
        /// </summary>
        [DataMember(Name = "Producer")]
        private readonly string producerAgentName;

        /// <summary>
        /// The agent we subscribe to.
        /// </summary>
        private IAgentHost producerAgent;

        /// <summary>
        /// Initializes a new instance of the <see cref="ConsumerProducerAgent{TInput,TOutput}"/> class. 
        /// Initializes a new instance of the ConsumerProducerAgent class.
        /// Empty constructor, to allow derived classes to have one as well.
        /// </summary>
        public ConsumerProducerAgent()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ConsumerProducerAgent{TInput,TOutput}"/> class. 
        /// Initializes a new instance of the ConsumerProducerAgent class.
        /// This constructor is not called when the agent is created from a manifest file.
        /// </summary>
        /// <param name="name">
        /// A friendly name for this agent instance.
        /// </param>
        /// <param name="producer">
        /// The name of the producer to hook up to.
        /// </param>
        public ConsumerProducerAgent(string name, string producer)
            : base(name)
        {
            this.producerAgentName = producer;
        }

        /// <summary>
        /// Gets the agent we subscribe to.
        /// </summary>
        protected IAgentHost ProducerAgent
        {
            get
            {
                return this.producerAgent;
            }
        }

        /// <summary>
        /// Called after construction, to allow the agent to finish initialization. 
        /// </summary>
        /// <param name="locator">A locator that can be used to find other agents.</param>
        public override void Initialize(AgentLocator locator)
        {
            base.Initialize(locator);

            this.producerAgent = locator.GetAgent(this.producerAgentName);
            this.producerAgent.Subscribe(this);
        }

        /// <summary>
        /// Called when the agent transitions from inactive to active.
        /// </summary>
        public override void OnActivated()
        {
            this.producerAgent.Activate();
        }

        /// <summary>
        /// Called when the agent transitions from active to inactive.
        /// </summary>
        public override void OnDeactivated()
        {
            this.producerAgent.Deactivate();
        }

        /// <summary>
        /// Called when a new input message is available.
        /// </summary>
        /// <param name="message">The new message to receive.</param>
        public abstract void Receive(TInput message);
    }
}
